package com.csd.serviceImpl;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.csd.dao.MessageDAO;
import com.csd.exception.DeleteException;
import com.csd.exception.GetException;
import com.csd.exception.SaveException;
import com.csd.helper.IConstant;
import com.csd.helper.SessionManager;
import com.csd.pojo.Manager;
import com.csd.pojo.Message;
import com.csd.pojo.Teacher;
import com.csd.pojo.Team;
import com.csd.service.IMessageService;
import com.csd.vo.UserVO;

/**
 * 
 * Service：消息
 * @author csd
 * @version 1.0
 * 
 * <br> 修订时间：2009-12-17
 * <br> 修改原因：创建文件
 * <br> 修改人：何文深
 * 
 * <br> 
 * 
 */
public class MessageService implements IMessageService{

	private MessageDAO messageDAO;

	public MessageDAO getMessageDAO() {
		return messageDAO;
	}

	public void setMessageDAO(MessageDAO messageDAO) {
		this.messageDAO = messageDAO;
	}
	
	/**
	 * 查询未读信息
	 * @return 信息对象列表
	 */
	public List<Message> listNewMessage(){
		List<Message> messageList = null;
		try {
			UserVO vo  = SessionManager.getUserVO();
			String receiverid = vo.getType()+vo.getUserID();
			List<Message> temp = messageDAO.findByReceiverid(receiverid);
			if(temp!=null && temp.size()>0)
			{
				messageList = new ArrayList();
				for(Message message:temp)
				{
					
					if(message.getIsnew() == true)
						messageList.add(message);
				}
			}
		} catch (RuntimeException re) {
			throw new GetException(re, "查询新消息异常");
		}
		return messageList;
	}
	
	/**
	 * 查询已读信息
	 * @return 信息对象列表
	 */
	public List<Message> listOldMessage(){
		List<Message> messageList = null;
		try {
			UserVO vo  = SessionManager.getUserVO();
			String receiverid = vo.getType()+vo.getUserID();
			List<Message> temp = messageDAO.findByReceiverid(receiverid);
			if(temp!=null && temp.size()>0)
			{
				messageList =  new ArrayList();
				for(Message message:temp)
				{
					if(message.getIsnew() == false)
						messageList.add(message);
				}
			}
		} catch (RuntimeException re) {
			throw new GetException(re, "查询旧消息异常");
		}
		return messageList;
	}
	
	/**
	 * 发送信息
	 * @param message 信息对象
	 * @param senderID 发送者ID
	 * @param receiverID 接受者id
	 * @param sendRole 发送者角色
	 * @param receiveRole 接受者角色
	 * @return 发送成功返回True
	 */
	public boolean sendMessage(Message message)
	{
		boolean bl = false;
		try {
			Date currentTime = new Date(System.currentTimeMillis());    
		 
			message.setTime(currentTime);
			UserVO vo = SessionManager.getUserVO();
			String senderid =  vo.getType()+vo.getUserID();
			message.setSenderid(senderid);
			message.setIsnew(true);
			messageDAO.save(message);
			bl = true;

		} catch (RuntimeException re) {
				throw new SaveException(re, "发送消息异常");
			}			
		return bl;
		
	}
	
	/**
	 * 删除信息
	 * @param messageID 信息id
	 * @return 删除成功返回true
	 */
	public boolean deleteMessage(Integer messageID)
	{
		boolean bl = false;
		Message message = new Message();
		try {
			message.setId(messageID);
			messageDAO.delete(message);
			bl = true;
		} catch (RuntimeException re) {
			throw new DeleteException(re, "删除消息异常");
		}
		return  bl;
	}
	
	/**
	 * 查询单条信息
	 * @param messageID 信息id
	 * @return 信息对象
	 */
	public Message searchMessage(Integer messageID)
	{
		Message message = null;
		try {
			UserVO vo = SessionManager.getUserVO();
			String receiver = vo.getType()+vo.getUserID();
			message = messageDAO.findById(messageID);
			if(message.getReceiverid().equals(receiver))
			{
				message.setIsnew(false);
				messageDAO.updateMessage(message);
			}
			
		} catch (RuntimeException re) {
			throw new GetException(re, "查询消息异常");
		}
		return  message;
	}

	public List<Message> listSendMessage() {
		List<Message> messageList = null;
		try {
			UserVO vo  = SessionManager.getUserVO();
			String senderid = vo.getType()+vo.getUserID();
			messageList = messageDAO.findBySenderid(senderid);
		} catch (RuntimeException re) {
			throw new GetException(re, "列出发送的消息异常");
		}
		return messageList;
	}
	
}
